﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace MendeleyClient.Phone
{
    public class PublicationDataBroker
    {
        PublicationViewModel publicationViewModel;
        WebClient webClient;
        string uriStr = "http://api.mendeley.com/oapi/stats/publications/?consumer_key=2c3b432027e4d19d95b69c1209b40d2a04e89f620";

        public PublicationDataBroker(PublicationViewModel publicationViewModel)
        {
            this.publicationViewModel = publicationViewModel;
            webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
        }

        public void GetData()
        {
            if (!webClient.IsBusy)
                webClient.DownloadStringAsync(new Uri(uriStr));
        }

        private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var records = AuthorRecord.Parse(e.Result.ToString());
            foreach (var record in records)
            {
                publicationViewModel.Items.Add(new PublicationItemViewModel() { Name = record.name, Value = record.value });
            }
            publicationViewModel.IsDataLoaded = true;

        }

        private void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
        }
    }
}
